<?php
namespace Controllers;

class indexController extends Controller
{
    // 前端做权限控制
    public function index($request, $respond, $args)
    {
        echo '打扰了';
    //     $id = $this->getUserId();
    //     $access = $this->getAccessList($id);
    //     // echo json_encode($access);
    //     $group = array();
    //     foreach ($access as $key1 => $value) 
    //     {
    //         foreach ($value as $key2 => $v) 
    //         {
    //             $node = $this->container['db']->select($this->nodeTable, '*', [
    //                 'id' => $v,
    //                 'ORDER' => [
    //                     'sort' => 'DESC'
    //                 ]
    //             ]);
    //             // var_dump($node);
    //             $tmp = array();
    //             $tmp['id'] = $node[0]['id'];
    //             $tmp['name'] = $node[0]['name'];
    //             $tmp['title'] = $node[0]['title'];
    //             $tmp['pid'] = $node[0]['pid'];
    //             $tmp['icon'] = '/img/icon.icon';                    
    //             // $tmp['icon'] = $node[0]['icon'];
    //             $tmp['url'] = '/api/node/'.$node[0]['name'];
    //             $tmp['children'] = '';
    //             $group[$key1][] = $tmp; 
    //         }
    //     }
    //         return json_encode($group);
    }

    public function getAccessList($authId)
    {
        $table = array('role' => $this->roleTable,
                        'user' => $this->roleUserTable,
                        'access' => $this->accessTable,
                        'node' => $this->nodeTable);
        $sql = "select node.id,node.name from ".
                $table['role']." as role,".
                $table['user']." as user,".
                $table['access']." as access ,".
                $table['node']." as node ".
                "where user.user_id='{$authId}' and user.role_id=role.id and ( access.role_id=role.id  or (access.role_id=role.pid and role.pid!=0 ) ) and role.status=1 and access.node_id=node.id and node.level=1 and node.status=1";
        $apps = $this->container['db']->query($sql)->fetchAll();
        $access =  array();
        foreach ($apps as $key => $app) 
        {
            $appId  =   $app['id'];
            $appName     =   $app['name'];
            // 读取项目的模块权限
            $access[strtoupper($appName)]   =  array();
            $sql    =   "select node.id,node.name from ".
                    $table['role']." as role,".
                    $table['user']." as user,".
                    $table['access']." as access ,".
                    $table['node']." as node ".
                    "where user.user_id='{$authId}' and user.role_id=role.id and ( access.role_id=role.id  or (access.role_id=role.pid and role.pid!=0 ) ) and role.status=1 and access.node_id=node.id and node.level=2 and node.pid={$appId} and node.status=1";
            $modules = $this->container['db']->query($sql)->fetchAll();
            $action = array();
            foreach ($modules as $a) 
            {
                $action[$a['name']] = $a['id'];
            }
            $access[strtoupper($appName)] = array_change_key_case($action, CASE_UPPER);
        }
        return $access;
    }
}
